﻿using AntdUI;
using ImportPlcPointPosition.Models.Table;
using ImportPlcPointPosition.Utils;
using NetTaste;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model;
using ZR.Model.Models.ProductionControl;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace ImportPlcPointPosition.Models
{
    [SugarTable("cks_plc_action")]
    public class CksPlcActions:BaseEntity
    {
        [SugarColumn(ColumnName = "Id", IsPrimaryKey = true, IsIdentity = true)]
        public int Id { get; set; }
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 1数据采集 2 工艺路线
        /// </summary>
        public int ActionType { get; set; } = 1;

        public string Notes { get; set; } = string.Empty;

        public int PlcId { get; set; }

        public int ReadDB { get; set; }

        public int WriteDB { get; set; }

        public int ActionId { get; set; }

        public string ActionKey { get; set; }
        

        public DateTime CreatedTime { get; set; } = DateTime.Now;
        /// <summary>
        /// 业务ID (ActionType不同代表含义不同  )
        /// </summary>
        public int BusinessId { get;  set; }

        #region Ignore
        private bool _IsCheck;
        [SugarColumn(IsIgnore = true)]
        public bool IsCheck {
            get => _IsCheck;
            set {
                _IsCheck = value;
                base.OnPropertyChanged();
                //OnPropertyChanged("AgvEnable");
            }
        }



        /// <summary>
        /// PLC
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public CellButton PlcIdCellText {
            get {
               
                var text = "--";

                var select = App.PlcConfigList.Where(o => o.Id == PlcId).SingleOrDefault();
                if (select != null) {
                    text = $"{select.Id}";
                }

                var array = App.PlcConfigList.Select(o => new SelectItem($"({o.Id})-{o.ProductionLineCode}-{o.WorkstationCode}-{o.Remark}", o)).ToArray();
                
                return new CellButton("", text) {
                    Ghost = true,
                    BorderWidth = 1,
                    //ShowArrow = true,
                    Tooltip = "单击下拉选择",
                    DropDownArrow = true,
                    //支持所有单元格控件
                    DropDownItems = array,
                    DropDownValueChanged = (value) => {
                        var obj = (value as PlcConfig);
                        PlcId = obj.Id;
                        OnPropertyChanged();
                    }
                };
            }
        }

        /// <summary>
        /// BlockDb
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public CellButton DBblockCellText {
            get {

                var text = "--";

                var dbList = SqlSugarHelper.MesDB.Queryable<PlcBlockDb>()
                    .Where(o => o.PlcId == PlcId)
                    .ToList();

                if (ReadDB > 0) {
                    text = ReadDB.ToString();
                } else {
                    var select = dbList.Where(o => o.PlcId == PlcId).FirstOrDefault();
                    if (select != null) {
                        text = $"{select.BlockDb}";
                        ReadDB = select.BlockDb;
                        WriteDB = select.BlockDb;
                    }
                }

                var array = dbList.Select(o => new SelectItem($"{o.BlockDb}", o)).ToList();
              
                return new CellButton("", text) {
                    Ghost = true,
                    BorderWidth = 1,
                    //ShowArrow = true,
                    Tooltip = "单击下拉选择",
                    DropDownArrow = true,
                    //支持所有单元格控件
                    DropDownItems = array.ToArray(),
                    DropDownValueChanged = (value) => {
                        var obj = (value as PlcBlockDb);
                        ReadDB = obj.BlockDb;
                        WriteDB = obj.BlockDb;
                        OnPropertyChanged();
                    }
                };
            }
        }

        #endregion
    }
}
